const express = require('express');
const router = express.Router();
const UserService = require('../services/UserService');

// 创建用户
router.post('/', (req, res) => {
  try {
    const { username, email, initialBalance } = req.body;
    
    // 验证输入
    if (!username || !email || !initialBalance) {
      return res.status(400).json({
        success: false,
        error: 'Username, email, and initialBalance are required'
      });
    }
    
    if (isNaN(initialBalance) || initialBalance <= 0) {
      return res.status(400).json({
        success: false,
        error: 'Initial balance must be a positive number'
      });
    }
    
    const user = UserService.createUser(username, email, parseFloat(initialBalance));
    
    res.status(201).json({
      success: true,
      data: user
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 获取特定用户
router.get('/:userId', (req, res) => {
  try {
    const { userId } = req.params;
    
    if (isNaN(userId)) {
      return res.status(400).json({
        success: false,
        error: 'User ID must be a number'
      });
    }
    
    const user = UserService.getUser(parseInt(userId));
    
    if (!user) {
      return res.status(404).json({
        success: false,
        error: 'User not found'
      });
    }
    
    res.json({
      success: true,
      data: user
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 获取所有用户
router.get('/', (req, res) => {
  try {
    const users = UserService.getAllUsers();
    
    res.json({
      success: true,
      data: users
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 用户买入股票
router.post('/:userId/buy', (req, res) => {
  try {
    const { userId } = req.params;
    const { symbol, quantity, price } = req.body;
    
    // 验证输入
    if (!symbol || !quantity || !price) {
      return res.status(400).json({
        success: false,
        error: 'Symbol, quantity, and price are required'
      });
    }
    
    if (isNaN(userId) || isNaN(quantity) || isNaN(price)) {
      return res.status(400).json({
        success: false,
        error: 'User ID, quantity, and price must be numbers'
      });
    }
    
    const result = UserService.buyStock(
      parseInt(userId), 
      symbol, 
      parseInt(quantity), 
      parseFloat(price)
    );
    
    res.json(result);
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 用户卖出股票
router.post('/:userId/sell', (req, res) => {
  try {
    const { userId } = req.params;
    const { symbol, quantity, price } = req.body;
    
    // 验证输入
    if (!symbol || !quantity || !price) {
      return res.status(400).json({
        success: false,
        error: 'Symbol, quantity, and price are required'
      });
    }
    
    if (isNaN(userId) || isNaN(quantity) || isNaN(price)) {
      return res.status(400).json({
        success: false,
        error: 'User ID, quantity, and price must be numbers'
      });
    }
    
    const result = UserService.sellStock(
      parseInt(userId), 
      symbol, 
      parseInt(quantity), 
      parseFloat(price)
    );
    
    res.json(result);
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

// 获取用户投资组合
router.get('/:userId/portfolio', (req, res) => {
  try {
    const { userId } = req.params;
    
    if (isNaN(userId)) {
      return res.status(400).json({
        success: false,
        error: 'User ID must be a number'
      });
    }
    
    const portfolio = UserService.getUserPortfolio(parseInt(userId));
    
    res.json({
      success: true,
      data: portfolio
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: error.message
    });
  }
});

module.exports = router;